#include "MCDrawEllipseCommand.h"
#include "MCEllipseVisual.h"
#include "MCLineSegment2D.h"

MCDrawEllipseCommand::MCDrawEllipseCommand(MCCommandManager *owner):
    MCCommand(owner),
    _currentStage(MCDrawEllipseStage::SetCenter_),
    _ellipseVisual(new MCEllipseVisual(presenter, MCEllipse2D())),
    _parametricWindow(nullptr)
{
    presenter->addVisualItem(_ellipseVisual);
}

MCDrawEllipseCommand::~MCDrawEllipseCommand()
{
    if (_parametricWindow != nullptr)
    {
        delete _parametricWindow;
    }
}

void MCDrawEllipseCommand::onMouseDown(IMouseArgs &args)
{
    if (args.isLeftMousePressed())
    {
        if (_currentStage == MCDrawEllipseStage::SetCenter_)
        {
           _settedCenter = args.globalPos();
           _ellipseVisual->update(_settedCenter, 0, 0);
           _currentStage = MCDrawEllipseStage::SetXRadius;
        }
        else if (_currentStage == MCDrawEllipseStage::SetXRadius)
        {
            _settedRx = MCLineSegment2D(args.globalPos(), _settedCenter).length();
            _ellipseVisual->update(_settedCenter, _settedRx, _settedRx);
            _currentStage = MCDrawEllipseStage::SetYRadius;
        }
        else if (_currentStage == MCDrawEllipseStage::SetYRadius)
        {
            _settedRy = MCLineSegment2D(args.globalPos(), _settedCenter).length();
            _ellipseVisual->update(_settedCenter, _settedRx, _settedRy);
            _currentStage = MCDrawEllipseStage::E_ParametricWindow;

            _parametricWindow = new MCEllipseParametricWindow(this, _ellipseVisual);
            _parametricWindow->show();
        }
    }
    else if (args.isRightMousePressed())
    {
        terminate();
    }
}

void MCDrawEllipseCommand::onMouseUp(IMouseArgs &args)
{

}

void MCDrawEllipseCommand::onMouseMove(IMouseArgs &args)
{
    if (_currentStage == MCDrawEllipseStage::SetXRadius)
    {
        _settedRx = MCLineSegment2D(args.globalPos(), _settedCenter).length();
        _ellipseVisual->update(_settedCenter, _settedRx, _settedRx); 
    }
    else if (_currentStage == MCDrawEllipseStage::SetYRadius)
    {
        _settedRy = MCLineSegment2D(args.globalPos(), _settedCenter).length();
        _ellipseVisual->update(_settedCenter, _settedRx, _settedRy); 
    }
}

void MCDrawEllipseCommand::terminate()
{

}
